<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Repositories\QuizRepository;

class Quiz extends Cachable
{
    protected $fillable = [
        'name', 'copper_limit', 'is_public', 'stop_at', 'type', 'group_id', 'options'
    ];

    public function setOptionsAttribute($value)
    {
        $this->attributes['options'] = is_array($value)
            ? json_encode($value) : '{}';
    }

    public function getOptionsAttribute($value)
    {
        return json_decode($value, true);
    }

    /**
     * 缓存中的俱乐部
     *
     * @return Group
     */
    public function getCachedGroupAttribute()
    {
        return Group::findCache($this->group_id, function ($query) {
            $query->with('creater');
        });
    }

    /**
     * 竞猜内容
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function items()
    {
        return $this->hasMany(QuizItem::class);
    }

    /**
     * 包含当前用户的下注情况
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function itemsWithCurrentBets()
    {
        return $this->hasMany(QuizItem::class)->with('currentBets');
    }


    /**
     * 俱乐部
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function group()
    {
        return $this->belongsTo(Group::class, 'group_id', 'id');
    }

    /**
     * 当前用户账户
     *
     * @return  \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function currentUserAccountInThisGroup()
    {
        return $this
            ->hasOne(GroupUser::class, 'group_id', 'group_id')
            ->where('user_id', request()->user()->id);
    }

    /**
     * 创建人账户
     *
     * @return  \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function createrAccountInThisGroup()
    {
        return $this
            ->hasOne(GroupUser::class, 'group_id', 'group_id')
            ->where('user_id', $this->user_id);
    }

    /**
     * 当前用户下注列表
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function currentBets()
    {
        return $this
            ->hasMany(Bet::class)
            ->where('user_id', request()->user()->id);
    }

    /**
     * 竞猜创建人
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function creater()
    {
        return $this->belongsTo(User::class, 'user_id', 'id')->select(['id', 'name' , 'avatar']);
    }

    /**
     * 竞猜参与人
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function players()
    {
        return $this
            ->hasMany(QuizUser::class)
            ->join('users', 'users.id', '=', 'quiz_user.user_id')
            ->select(['users.id', 'users.name', 'users.avatar'])
            ->where('user_id', '!=', $this->user_id)
            ->orderBy('quiz_user.updated_at', 'desc');
    }

    /**
     * 开奖后结果
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function statement()
    {
       return $this
           ->hasOne(Statement::class, 'model_id', 'id')
           ->where('model_type', 'quiz');
    }

    /**
     * 投注结果
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function finishedBets()
    {
        return $this
            ->hasMany(Bet::class)
            ->with('user')
            ->where('status', QuizRepository::BET_STATUS_FINISHED);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function bets()
    {
        return $this->hasMany(Bet::class)->whereIn('status', [
            QuizRepository::BET_STATUS_PROCESSING,
            QuizRepository::BET_STATUS_PROCESSED,
        ]);
    }
}
